import flash.display.Shape;
import SquareModel;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;

class PlayerSquareView extends AVerletView
{
  private var _model:SquareModel;
  private var _controller:SquareController;
  private var _stage:Dynamic;
  //private override var model:SquareModel;
  public function new(squareModel:SquareModel,controller:SquareController,stage:Dynamic)
  {
    super(squareModel);
    _controller = controller;
    _stage = stage;
    _stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
    _stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
    draw();
  }
  override private function changeHandler(event:Event)
  {
    _controller.processUpdate();
    this.alpha = cast(model,SquareModel).Alpha;
    super.changeHandler(event);
  }

  private function keyDownHandler(event:KeyboardEvent)
  {
    _controller.processKeyDown(event);
  }
  private function keyUpHandler(event:KeyboardEvent)
  {
    _controller.processKeyUp(event);
  }
  private function mouseDownHandler(event:MouseEvent)
  {
    _controller.processMouseDown(event, _stage);
  }
  private function mouseUpHandler(event:MouseEvent)
  {
    _controller.processMouseUp(event);
  }

  override private function draw()
  {
    var shape:Shape = new Shape();
    shape.graphics.lineStyle(1);
    shape.graphics.beginFill(model.color);
    shape.graphics.drawRect(0, 0, model.width,model.height);
    shape.graphics.endFill();

    addChild(shape);
  }
}